const { queryDatabase } = require('../queue-client');

// 获取既无货又没有未下发任务的货位
async function getAvailableSlots() {
  try {
    console.log('开始执行可用货位查询');
    
    const sql = `
      SELECT s.* 
      FROM storage_slots s
      LEFT JOIN TaskList t ON s.row = t.Unload_Line 
        AND s.column = t.Unload_Col 
        AND s.layer = t.Unload_Layer 
        AND t.is_issued = 0
      WHERE s.has_goods = 0 
        AND t.id IS NULL
      ORDER BY s.row, s.column, s.layer
    `;
    
    const rows = await queryDatabase(sql);
    console.log(`查询到 ${rows.length} 个可用货位`);
    return rows;
  } catch (error) {
    console.error('查询可用货位失败:', error);
    throw error;
  }
}

module.exports = {
  getAvailableSlots
};